package com.znsd.utils;


import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.junit.Test;

public class EmailUtils {
	/**
	 * @param to
	 * @param text
	 * @param title
	 * @throws AddressException 
	 */
	/* 发送验证信息的邮件 */
	public static void sendMail(String to,String contentx) throws AddressException {
		Properties prop = System.getProperties();
		// 设置SMTP服务器
		prop.setProperty("mail.host", "smtp.qq.com");
		// 声明邮件所使用的协议
		prop.setProperty("mail.transport.protocol", "smtp");
		// 开启邮箱安全验证
		prop.setProperty("mail.smtp.auth", "true");
		// 开启邮箱使用SSL加密协议
		prop.setProperty("mail.smtp.ssl.enable", "true");
		// 设置SMTP服务器端口号
		prop.setProperty("mail.smtp.port", "465");
		Session session = Session.getDefaultInstance(prop);
		session.setDebug(true);

		// 创建Message对象
		MimeMessage message = new MimeMessage(session);
		try {
			// 设置发件人
			message.setFrom(new InternetAddress("363599496@qq.com"));
			//设置收件人
			// 设置收件人
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			// 设置邮件标题
			message.setSubject("就很棒");
			// 设置邮件能容
			Multipart multipart = new MimeMultipart();
			// 设置邮件的文本内容
			BodyPart contentPart = new MimeBodyPart();
			contentPart.setContent(contentx, "text/html;charset=utf-8");
			multipart.addBodyPart(contentPart);
			message.setContent(multipart);
			message.saveChanges();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		String username = "363599496@qq.com";
		String password = "lzoulgevzpzncach";
		Transport transport;
		try {
			// 发送邮件
			transport = session.getTransport();
			transport.connect(username, password);
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
	/*
	 * @Test public void t() { try { sendMail("2549397439@qq.com","nn"); } catch
	 * (AddressException e) { // TODO Auto-generated catch block
	 * e.printStackTrace(); } }
	 */
}
